#pragma once
#include <iostream>
#include <cstdlib>

#define TASKNNUM 3

typedef void (*task_t)();
task_t tasks[TASKNNUM];

void Print() {
    std::cout << "I am print task" << std::endl;
}

void DownLoad() {
    std::cout << "I am a dowmload task" << std::endl;
}

void Flush() {
    std::cout << "I am a flush task" << std::endl;
}

void LoadTask() {
    srand(time(0) ^ getpid());
    tasks[1] = DownLoad;
    tasks[2] = Flush;
    tasks[0] = Print;

}

size_t SelectTask() {
    int selectnum = rand() % TASKNNUM;
    return selectnum;
}

void SubReadCommand(std::string name) {
    while (true) {
        int command = 1;
        int n = read(0, &command, sizeof(int));
        if (n == sizeof(int)) {
            // 执行对应的程序
            std::cout << "I am " << name << std::endl;
            size_t taskindex = command;
            tasks[taskindex]();
        } else if (n == 0) {
            // 读到0个数，说明读端已经关闭
            std::cout << "sub process : " << getpid() << " quit" << std::endl;
            break;
        }
    }
}